package com.godyao.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.godyao.enums.ResultMsg;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author godyao
 * @date 2022/3/16
 */
public class ResponseUtils {
    public static void result(HttpServletResponse response, ResultMsg resultMsg) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        final ServletOutputStream outputStream = response.getOutputStream();
        final ObjectMapper objectMapper = new ObjectMapper();
        final byte[] bytes = objectMapper.writeValueAsString(resultMsg).getBytes(StandardCharsets.UTF_8);
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }
}
